//
//  HomeAPI.m
//  RainbowFM
//
//  Created by Kratos on 14/12/20.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "HomeAPI.h"
#import "AFAppDotNetAPIClient.h"


@implementation HomeAPI


-(void)fetchHomeStructure:(void (^)(NSArray *structure, NSString *msg))res
{
    
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@"2" forKey:@"model"];
    [params setObject:@"100" forKey:@"channelId"];
    [params setObject:currentVersion forKey:@"curVer"];
    
    [[AFAppDotNetAPIClient sharedClient] POST:SPMS_BASE_URL
                                   parameters:@{@"serviceName":@"frame_queryHomeStructure_req",
                                                @"callType":@"002",
                                                @"params":params
                                                }
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(responseObject[@"result"][@"list"],nil);
                                          }
                                          else{
                                              res(nil,responseObject[@"errorMsg"]);
                                          }
                                          
                                      }
                                      failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];

}

-(void)fetchEdit:(void (^)(NSArray *structure, NSString *msg))res
{
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@"2" forKey:@"model"];
    [params setObject:@"100" forKey:@"channelId"];
    [params setObject:currentVersion forKey:@"curVer"];
    
    [[AFAppDotNetAPIClient sharedClient] POST:SPMS_BASE_URL
                                   parameters:@{@"serviceName":@"frame_queryEdit_req",
                                                @"callType":@"002",
                                                @"params":params
                                                }
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(responseObject[@"result"],nil);
                                          }
                                          else{
                                              res(nil,responseObject[@"errorMsg"]);
                                          }
                                          
                                      }
                                      failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];

}

-(void)sendMsgWithPhone:(NSString *)phone content:(NSString *)content result:(void (^)(NSDictionary *result, NSString *msg))res;
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (!phone || !content) {
        res(nil,@"缺少 电话 或 发送内容");
        return;
    }
    
    [params setObject:phone forKey:@"phone"];
    [params setObject:content forKey:@"content"];
    
    [[AFAppDotNetAPIClient sharedClient] POST:SPMS_BASE_URL
                                   parameters:@{@"serviceName":@"aam_sendMsg _req",
                                                @"callType":@"002",
                                                @"params":params
                                                }
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(responseObject[@"result"],nil);
                                          }
                                          else{
                                              res(nil,responseObject[@"errorMsg"]);
                                          }
                                          
                                      }
                                      failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];
}

-(void)querySpeed:(void (^)(NSDictionary *result, NSString *msg))res
{
    [[AFAppDotNetAPIClient sharedClient] POST:SPMS_BASE_URL
                                   parameters:@{@"serviceName":@"aam_sendMsg _req",
                                                @"callType":@"002",
                                                @"params":@{}
                                                }
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(responseObject[@"result"],nil);
                                          }
                                          else{
                                              res(nil,responseObject[@"errorMsg"]);
                                          }
                                          
                                      }
                                      failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];
}

@end
